import { IActions, IEvents } from './interfaces';
import get_emit from './utils/get-emit';

export default function init(data: any, actions: IActions, events: IEvents) {
	// 执行事件对应的js响应函数，或者规则编号对应的响应函数
	async function fire_action(action: string, ...args: any[]) {
		const fun = actions[action];
		if (!fun) {
			return;
		} else {
			const feidao_data = { emit, fire, fire_action, fire_nools, data } as any;
			return await fun(feidao_data, ...args);
		}
	}

	// 执行事件对应的规则响应
	async function fire_nools() {
		throw new Error('Could not fire nools');
	}

	function fire(action: string, ...args: any[]) {
		return fire_action(action, ...args);
	}

	const emit = get_emit(events, fire);
	// Object.freeze(data);	// Could not freeze data here, see page.ts

	return Object.freeze({ emit, fire, fire_action, fire_nools, data });
}
